package org.qiyu.live.common.interfaces.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

/**
 * @Program: qiyu-live-app
 *
 * @Description:
 *
 * @Author: zx
 *
 * @Create: 2024-08-12 17:52
 */
public class ListUtils {

    /**
     * 生成红包金额(二倍随机法)
     *
     * @param totalCount
     * @param totalPrice
     * @return
     */
    public static List<Integer> createRedPacketPriceList(Integer totalCount, Integer totalPrice) {
        List<Integer> redPacketPriceList = new ArrayList<>();
        for (int i = 0; i < totalCount; i++) {
            //最后一个红包
            if (totalCount == i + 1) {
                redPacketPriceList.add(totalPrice);
                break;
            }
            int maxLimit = (totalPrice / (totalCount - i)) * 2;
            int currentPrice = ThreadLocalRandom.current().nextInt(1, maxLimit);
            totalPrice -= currentPrice;
            redPacketPriceList.add(currentPrice);
        }
        return redPacketPriceList;
    }


    /**
     * 将一个List集合拆解为多个子List集合
     *
     * @param list
     * @param subNum
     * @return
     * @param <T>
     */
    public static <T> List<List<T>> splitList(List<T> list, int subNum) {
        List<List<T>> tNewList = new ArrayList<>();
        int priIndex = 0;
        int lastPriIndex = 0;
        int insertTimes = list.size() / subNum;
        List<T> subList;
        for (int i = 0; i <= insertTimes; i++) {
            priIndex = subNum * i;
            lastPriIndex = priIndex + subNum;
            if (i == insertTimes) {
                subList = list.subList(priIndex, list.size());
            } else {
                subList = list.subList(priIndex, lastPriIndex);
            }
            if (subList.size() > 0) {
                tNewList.add(subList);
            }
        }
        return tNewList;
    }

}
